package server;

import javax.jms.JMSException;
import javax.jms.ObjectMessage;
import javax.jms.Queue;
import javax.jms.QueueSender;
import javax.jms.QueueSession;
import javax.jms.Session;

import job.Job;

public class TopicBasedThread extends Thread {

	private TopicBasedServer server;
	private Job job;
	private Queue replyTo;
	

	public TopicBasedThread(TopicBasedServer server, Job job, Queue replyTo) {
		this.server = server;
		this.job = job;
		this.replyTo = replyTo;
		
	}

	public void run() {
		try {
			
			QueueSession qs = server.getQueueConnection().createQueueSession(false, Session.AUTO_ACKNOWLEDGE);
			
			job.run();
			
			ObjectMessage reply = qs.createObjectMessage();
			QueueSender qsend = qs.createSender(null);

			reply.setObject(job);
			qsend.send(replyTo, reply);
			qsend.close();
			
			server.removeJob(job.getName());
			
			//System.out.println("job executed - LOAD: " + server.getRunningJobs());
			
			server.shout();			
			server.verify();
			
		} catch (JMSException e) {
			e.printStackTrace();
		}
	}

}